package com.boot.util;

import com.boot.dao.JdbcTemplate;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by xudi on 5/9/16.
 */
public class BatchInsertGoddessInfo extends JdbcTemplate {

    public static void main(String[] args) throws SQLException {
        new BatchInsertGoddessInfo().insert();
//        for (int i = 0; i < 100; i++) {
//            System.out.println(randomLonLat(120.51,120.52,30.40,30.41));
//        }
    }

    public void insert() throws SQLException {
        Connection postgresqlConn = DBHelper.getPostgresqlConnection();
        Connection mysqlConn = DBHelper.getMysqlConnection();

        String insertAccountSql = "insert into account(id,type) values(?,?);";

        String insertUserSql = "insert into user_info(account_id,uuid,nick_name) " +
        "values(?,?,?);";

        String insertGoddessSql = "insert into goddess_info(account_id,fans_num,school) " +
                "values(?,?,?);";

        String insertGpsSql = "insert into building_gps_info(building_id,jwd,dt,building_name) " +
                "values(?,ST_Transform(ST_GeomFromText(?, 4326),2163),current_timestamp,?);";


        PreparedStatement gps = null;
        PreparedStatement account = null;
        PreparedStatement user = null;
        PreparedStatement goddess = null;
        try {
            gps =postgresqlConn.prepareStatement(insertGpsSql);
            account =mysqlConn.prepareStatement(insertAccountSql);
            user =mysqlConn.prepareStatement(insertUserSql);
            goddess =mysqlConn.prepareStatement(insertGoddessSql);
            for (int i=9000000;i<=9000200;i++) {

                if(true){
                    String jwd=randomLonLat(120.51,120.52,30.40,30.41);
                    String point="POINT("+jwd+")";
                    gps.setInt(1, i);
                    gps.setString(2, point);
                    gps.setString(3, point);
                    gps.executeUpdate();
                }

                if(false) {
                    account.setInt(1, i);
                    account.setInt(2, 6);
                    account.executeUpdate();
                }

                if(false) {
                    user.setInt(1, i);
                    user.setInt(2, i);
                    user.setString(3,"jose"+i);
                    user.executeUpdate();
                }

                if(false) {
                    goddess.setInt(1, i);
                    goddess.setInt(2, i);
                    goddess.setString(3, "felix"+i);
                    goddess.executeUpdate();
                }
                System.out.println("done!!!"+i);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            free(null, gps, postgresqlConn);
            free(null, user, mysqlConn);
            free(null, goddess, mysqlConn);
            free(null, account, mysqlConn);
        }
    }

    public static String randomLonLat(double min1, double max1,double min2, double max2) {
        BigDecimal db = new BigDecimal(Math.random() * (max1 - min1) + min1);
        String lon = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
        db = new BigDecimal(Math.random() * (max2 - min2) + min2);
        String lat = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
        return lon+" "+lat;
    }
}
